const colors = require("colors-console");
const fs = require("fs");
const process = require("child_process");

exports.copyObj = function (obj) {
    if (obj === null) return null;
    if (obj.constructor !== "object") return obj;
    if (obj.constructor === Date) return new Date(obj);
    if (obj.constructor === RegExp) return new RegExp(obj);
    var newObj = new obj.constructor(); //保持继承的原型
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            var val = obj[key];
            newObj[key] = typeof val === "object" ? exports.copyObj(val) : val;
        }
    }
    return newObj;
};

exports.copyJson = function (json) {
    let newObj = JSON.parse(JSON.stringify(json));
    return newObj;
};

exports.logColor = function (color = "red") {
    let args = [];
    for (let i = 1; i < arguments.length; i++) {
        args.push(arguments[i]);
    }
    console.log.apply(console, [colors(color, args[0])].concat(args.slice(1)));
};

exports.logError = function () {
    const color = "red";
    const self = this;
    let args = [];
    for (let i = 0; i < arguments.length; i++) {
        args.push(arguments[i]);
    }
    exports.logColor.apply(self, [color].concat(args));
};

exports.logSuccess = function () {
    const color = "green";
    const self = this;
    let args = [];
    for (let i = 0; i < arguments.length; i++) {
        args.push(arguments[i]);
    }
    exports.logColor.apply(self, [color].concat(args));
};

exports.logInfo = function () {
    const color = "blue";
    const self = this;
    let args = [];
    for (let i = 0; i < arguments.length; i++) {
        args.push(arguments[i]);
    }
    exports.logColor.apply(self, [color].concat(args));
};

exports.logTips = function () {
    const color = "yellow";
    const self = this;
    let args = [];
    for (let i = 0; i < arguments.length; i++) {
        args.push(arguments[i]);
    }
    exports.logColor.apply(self, [color].concat(args));
};

exports.logTrace = function () {
    const color = "red";
    const self = this;
    let args = [];
    for (let i = 0; i < arguments.length; i++) {
        args.push(arguments[i]);
    }
    console.trace.apply(console, [colors(color, args[0])].concat(args.slice(1)));
};

/**
 * 配置文件写入
 * @param {string} filePath
 * @param {string} content
 * @param {boolean} append 写入模式 追加/覆盖
 */
exports.writeFile = function (filePath, content, append, format = false) {
    let str = typeof content == "string" ? content : JSON.stringify(content);
    try {
        if (append) {
            fs.appendFileSync(filePath, str, {
                encoding: "utf8",
            });
        } else {
            fs.writeFileSync(filePath, str, {
                encoding: "utf8",
            });
        }

        if (format) {
            let exeErr = process.execSync("prettier --write " + filePath);
            if (exeErr) {
                console.error(exeErr.toString());
            }
        }
        console.log(colors("green", filePath));
        console.log(colors("green", "---success---\n"));
    } catch (e) {
        console.error("新配置写入失败\n", e);
        console.log(colors("red", filePath));
        console.log(colors("red", "---error---\n"));
    }
};

/**
 * 读取文件
 * @param {string} filePath
 * @returns
 */
exports.readFile = function (filePath) {
    try {
        return fs.readFileSync(filePath, { encoding: "utf8" });
    } catch (e) {
        console.error("文件读取失败\n", e);
        logError(filePath);
        logError("---error---\n");
    }
};

exports.readObj = function (filePath) {
    let content = exports.readFile(filePath);
    if (content) {
        return JSON.parse(content);
    }
    return null;
};

/**
 * 提取数字
 * @param {string} str
 */
exports.extractNum = function (str) {
    let rgx = /[0-9]{1,}/g;
    let strs = str.match(rgx);
    if (strs && strs.length) {
        return +strs[0];
    }
    return 0;
};
